<p>This rule raises an issue when async operations are wrapped in immediately invoked function expressions, promise chains, or async functions that
are called immediately at the top level.</p>
<h2>Why is this an issue?</h2>
<p>JavaScript’s top-level await feature allows you to use <code>await</code> directly at the module level without wrapping async operations in
functions. This makes code cleaner and more readable.</p>
<p>When you wrap async operations in immediately invoked function expressions (IIFEs) or create async functions just to call them immediately, you add
unnecessary boilerplate code. This pattern was common before top-level await was available, but is no longer needed in modern JavaScript
environments.</p>
<p>Using wrapper patterns can also make error handling more complex and may lead to unhandled promise rejections if not properly managed. Top-level
await provides a more straightforward approach to handling async operations at the module level.</p>
<h3>What is the potential impact?</h3>
<p>Using wrapper patterns instead of top-level await reduces code readability and maintainability. It adds unnecessary complexity and boilerplate code
that makes the intent less clear. In some cases, improper error handling in wrapper patterns can lead to unhandled promise rejections.</p>
<h3>How to fix?</h3>
<p>Replace immediately invoked async function expressions with top-level await.</p>
<h4>Non-compliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
(async () =&gt; {
  try {
    await run();
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
})(); // Noncompliant
</pre>
<h4>Compliant code example</h4>
<pre data-diff-id="1" data-diff-type="compliant">
try {
  await run();
} catch (error) {
  console.error(error);
  process.exit(1);
}
</pre>
<h3>Documentation</h3>
<ul>
  <li> <a href="https://github.com/sindresorhus/eslint-plugin-unicorn#readme">eslint-plugin-unicorn</a> - Rule <a
  href="https://github.com/sindresorhus/eslint-plugin-unicorn/blob/HEAD/docs/rules/prefer-top-level-await.md">prefer-top-level-await</a> </li>
  <li> MDN - Top-level await - <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#top_level_await">Official
  documentation for JavaScript’s top-level await feature</a> </li>
  <li> TC39 - Top-level await proposal - <a href="https://github.com/tc39/proposal-top-level-await">The official ECMAScript proposal for top-level
  await</a> </li>
</ul>
